package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.TargetService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/target")
public class TargetController {
    @Autowired
    TargetService targetService;

    //分页查询和模糊查询
    @RequestMapping("/target-list")
    @ResponseBody
    public Map<String,Object> allTarget(Integer pageNum, Integer pageSize, String targetCjsj, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum, pageSize);

        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }
        //分割时间
        List<Target> targetList = targetService.selecttarget(targetCjsj.substring(0,4),empId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",targetList);
        return map;
    }

    //统计图销售目标金额和业绩金额查询
    @RequestMapping("/target-jechaxu")
    @ResponseBody
    public Map<String,Object> targetje(String targetCjsj,HttpSession session){
        Map<String,Object> map = null;
        System.out.println("sj"+targetCjsj.substring(0,4));
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }
        map = targetService.empMbYj(targetCjsj.substring(0,4),empId);
        return map;
    }

    //统计图销售全部目标金额和全部业绩金额查询
    @RequestMapping("/target-quanbuje")
    @ResponseBody
    public Map<String,Object> MbYj(String nian){
        Map<String,Object> map = null;
        System.out.println("sj"+nian.substring(0,4));
        map = targetService.MbYj(nian.substring(0,4));
        return map;
    }

    //销售目标新增
    @RequestMapping("/add-target")
    @ResponseBody
    public void addTarget(@RequestBody SalesTargetVO salesTargetVO){
            targetService.addTarget(salesTargetVO);
    }
}
